/**
 * Licensed to Open-Ones Group under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Open-Ones Group licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package openones.mvcportlet.sample.validation;

import java.io.IOException;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import openones.corewa.BaseOutForm;
import openones.corewa.ReqUtil;
import openones.corewa.validate.config.ValidationConf;
import openones.mvcportlet.sample.validation.form.AddForm;
import rocky.mvcportlet.control.BaseControl;

/**
 * @author ThachLN
 *
 */
public class AddControl extends BaseControl {

    @Override
    public BaseOutForm init(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        // Prepare data for initial screen
        //outForm. 
        
        return outForm;
    }

    /**
     * Process add event.
     * @param request original request of the portlet
     * @param reqMap HashMap contain data from the jsp form.
     * @param validationConf data validation configuration
     * @param response original response of the portlet
     * @return Output form
     * @throws PortletException
     * @throws IOException
     */
    public BaseOutForm procAdd(ActionRequest request, Map<String, Object> reqMap, ValidationConf validationConf, ActionResponse response)
            throws PortletException, IOException {
        LOG.debug("procAdd.START; validationConf=" + validationConf);
        AddForm form = (AddForm) ReqUtil.getData(reqMap, AddForm.class, validationConf);
        
        outForm.putRequest("formBean", form);
        
        if ((form.getErrors() != null) && (form.getErrors().size() > 0)) {
            LOG.debug("Errors " + form.getErrors().size() + ". Re-display the Add screen to display error");
            
            outForm.putRequest("errors", form.getErrors());
            outForm.setNextResult("fail");
        } else {
            LOG.debug("Setting result OK");
            Biz biz = new Biz();
            biz.addStudent(form);
            outForm.setNextResult("ok");
            outForm.putRequest("studentList", biz.getStudentList());
        }
        
        return outForm;
    }

}
